package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.rule.template.RuleTemplateDTO;
import io.github.talelin.latticy.service.RuleTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.RuleTemplateDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
 * @author generator@wulongbo
 * @since 2022-08-25
 */
@RestController
@RequestMapping("/v1/rule-template")
@Validated
@PermissionModule(value = "模板引擎")
public class RuleTemplateController {

    @Autowired
    private RuleTemplateService ruleTemplateService;

    @PostMapping("")
    @AdminRequired
    @PermissionMeta(value = "添加模板引擎", mount = false)
    public CreatedVO create(@RequestBody @Validated RuleTemplateDTO dto) {
        ruleTemplateService.addTemplate(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "修改模板引擎", mount = false)
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated RuleTemplateDTO dto) {
        ruleTemplateService.updateTemplateById(dto,id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "删除模板引擎", mount = false)
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        ruleTemplateService.deleteTemplateById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "通过id查询模板引擎", mount = false)
    public RuleTemplateDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return  ruleTemplateService.getTemplateById(id);
    }

    @GetMapping("/page")
    @AdminRequired
    @PermissionMeta(value = "查询所有模板引擎", mount = false)
    public PageResponseVO<RuleTemplateDO> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count
    ) {
        IPage<RuleTemplateDO> iPage = ruleTemplateService.getTemplatePage(page, count);
        return PageUtil.build(iPage);
    }

}
